package picol.view.component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.CaretListener;
import javax.swing.table.DefaultTableCellRenderer;
import picol.controller.Controler;

/**
 *
 * @author Aurélien Marcel - Julien Sanderson
 */
public class PanelEditor extends JPanel implements ViewComponent {

    private JScrollPane scrollEditor;
    private JScrollPane scrollLineCount;
    private JTable tableCountRow;
    private JScrollBar scrollBarEditor;
    private ModelTableCounter modelTable;
    private int counterList;
    private JTextArea textArea;
    private DefaultTableCellRenderer custom;

    /**
     * Default Constructor
     */
    public PanelEditor() {
        this.init();
    }

    private void init() {
        this.counterList = 0;
        this.scrollBarEditor = new JScrollBar();

        //TextArea
        this.textArea = new JTextArea(20, 65);
        this.textArea.setSize(3, 10);
        this.textArea.setTabSize(2);
        this.scrollEditor = new JScrollPane(this.textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        this.scrollEditor.setVerticalScrollBar(this.scrollBarEditor);

        //table
        this.modelTable = new ModelTableCounter();
        this.tableCountRow = new JTable(modelTable);
        this.tableCountRow.getColumnModel().getColumn(0).setMaxWidth(10);
        this.tableCountRow.getColumnModel().getColumn(1).setMaxWidth(35);

        this.tableCountRow.setBackground(Color.LIGHT_GRAY);

        //text aligment
        custom = new DefaultTableCellRenderer();
        custom.setHorizontalAlignment(JLabel.RIGHT);
        this.tableCountRow.getColumnModel().getColumn(1).setCellRenderer(custom);
        this.tableCountRow.setDefaultRenderer(Boolean.class, new TableCounter());
        this.tableCountRow.getColumnModel().getColumn(0).setCellRenderer(new TableCounter());

        this.scrollLineCount = new JScrollPane(this.tableCountRow, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.tableCountRow.setShowHorizontalLines(false);
        this.tableCountRow.setShowVerticalLines(false);
        this.scrollLineCount.setAutoscrolls(true);
        this.scrollLineCount.getVerticalScrollBar().setModel(this.scrollEditor.getVerticalScrollBar().getModel());
        this.scrollLineCount.getVerticalScrollBar().setVisible(false);
        this.scrollLineCount.setPreferredSize(new Dimension(55, 325));

        this.addComponent(this.scrollLineCount, 0, 0);
        this.addComponent(this.scrollEditor, 1, 0);
    }

    /**
     * Add component on the main panel
     * @param _component
     * @param _x
     * @param _y
     */
    protected void addComponent(JComponent _component, int _x, int _y) {
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = _x;
        constraints.gridy = _y;
        this.add(_component, constraints);
    }

    public void subscription(Controler _controler) {
        this.textArea.addCaretListener((CaretListener) _controler);
        this.tableCountRow.addMouseListener((MouseListener) _controler);
    }

    public void setLineCount() {
        this.modelTable.addLine(this.counterList);
    }

    public int getCounterList() {
        return this.counterList;
    }

    public void setCounterList(int _count) {
        this.counterList = _count;
    }

    public int getLineCount() {
        return this.textArea.getLineCount();
    }

    public String getText() {
        return this.textArea.getText();
    }

    public void setText(String _text) {
        this.textArea.setText(_text);
    }

    public int getLineClicked() {
        return this.tableCountRow.getSelectedRow();
    }

    public void setClickedLine(int line) {
        Boolean value = !(Boolean) this.tableCountRow.getValueAt(line, 0);
        this.tableCountRow.setValueAt(value, line, 0);
    }
}
